package com.chenyuhan.myview.testview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.chenyuhan.myview.R;

/**
 * Created by Administrator on 2017/7/12 0012.
 */

public class TestSwitchView extends View {
    private final String TAG = TestSwitchView.class.getSimpleName();

    private Paint mPaint;//画笔
    private Path mPath;//路径

    public TestSwitchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //关闭该view的硬件加速
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint();
        mPath = new Path();

    }

    public TestSwitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        //控件宽
        int vWidth = 0;
        switch (widthMode) {
            case MeasureSpec.EXACTLY:
                vWidth = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                vWidth = widthSize;
                break;
        }

        //控件高
        int vHeight = 0;
        switch (heightMode) {
            case MeasureSpec.EXACTLY:
                vHeight = (int) (vWidth * 0.55f);
                break;
            case MeasureSpec.AT_MOST:
                //控件高为宽的0.65倍
                vHeight = (int) (vWidth * 0.55f);
                break;
        }

        setMeasuredDimension(vWidth, vHeight);
    }

    //控件宽高
    private int mWidth, mHeight;
    //控件中心点坐标
    private int centerX, centerY;
    //左右半圆右下角点的横纵坐标
    private int lWidth, lHeight, rWidth, rHeight;

    //按钮的半径,边框宽
    private float bRadius, bStrokWidth;
    //按钮宽
    private float bWidth;
    //按钮范围四个点的坐标
    private float bLeft, bRight, bTop, bBottom;
    //缩放比例常量（防止缩放后没有边界标志）
    private float mScale;
    //按钮点击变化时变化点横坐标
    private float sScaleCenterX;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        //设置视图宽高
        mWidth = w;
        mHeight = h;
        //左半圆的右下角点横坐标（高度等于圆直径，所以横坐标等于圆直径）
        lWidth = mHeight;
        //左半圆的右下角点纵坐标（也等于直径）
        lHeight = mHeight;
        //右半圆的右下角点横坐标(等于宽度)
        rWidth = mWidth;
        //右半圆的右下角点纵坐标(等于直径)
        rHeight = mHeight;
        centerX = mWidth / 2;
        centerY = mHeight / 2;

        //画左半圆
        RectF rectF = new RectF(0, 0, lWidth, lHeight);
        //截取90度开始旋转了180度的角范围
        mPath.arcTo(rectF, 90, 180);
        //画右半圆(设置右半圆左上角顶点的横坐标，横坐标等于总宽度减去直径)
        rectF.left = mWidth - mHeight;
        //设置右半圆右下角的横坐标（为控件宽）
        rectF.right = mWidth;
        mPath.arcTo(rectF, 270, 180);
        mPath.close();//封闭路径

        //圆形按钮
        bLeft = bTop = 0;
        bRight = bBottom = mHeight;
        bWidth = bRight - bLeft;//控件宽
        final float halfHeightOfS = lHeight / 2;//控件一半高
        bRadius = 0.9f * halfHeightOfS;//圆形按钮半径
        bStrokWidth = halfHeightOfS - bRadius;//按钮的边框宽(一边)
        mScale = 1 - bStrokWidth / mHeight;//缩放比例
        sScaleCenterX = halfHeightOfS + bRadius + bStrokWidth;//算出放大缩小时的中心横坐标（为减掉圆形按钮后所剩宽度的一半）

    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.green));
        canvas.drawPath(mPath, mPaint);
        //保存画布状态，防止接下来的操作对之前画的内容有影响
        canvas.save();

        //设置画笔
        mPaint.setColor(getResources().getColor(R.color.white));
        sAnim = sAnim - 0.1f > 0 ? sAnim - 0.1f : 0;
        //计算缩放大小,乘以0.9是为了让周围有一层外圈，防止颜色不明显时看不出控件大小(isOn=true时是缩小，false是放大)
        float scale = mScale * (isOn ? sAnim : 1 - sAnim);
        canvas.scale(scale, scale, sScaleCenterX, centerY);
        canvas.drawPath(mPath, mPaint);
        //取出画布状态
        canvas.restore();

        //保存画布状态，防止接下来的操作对之前画的内容有影响
        canvas.save();
        //移动画布
        float bTranslateX = mWidth - bWidth;
        float translateX = bTranslateX * (isOn ? (1 - sAnim) : sAnim);// 平移距离参数随sAnim变化而变化
        canvas.translate(translateX, 0);
        //画按钮圆
        canvas.drawCircle(bWidth / 2, mHeight / 2, bRadius, mPaint);
        //画圆圈周围的灰边
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(getResources().getColor(R.color.lightgray));
        mPaint.setStrokeWidth(bStrokWidth);
        //加上+bStrokWidth/2是因为画笔是从其宽度的中心下笔的
        canvas.drawCircle(bWidth / 2, mHeight / 2, bRadius + bStrokWidth / 2, mPaint);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.white));
        //取出画布状态
        canvas.restore();

        if (sAnim > 0) invalidate();
    }

    //动画内圈渐变数
    private float sAnim;
    //开关状态标示
    private boolean isOn = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                sAnim = 1;
                isOn = !isOn;
                invalidate();
                break;
        }
        return true;
    }
}
